﻿// <copyright file="Global.asax.cs" company="Ecole Normale Supérieure">
// Released under GPLv2
// </copyright>
// <summary>Global.asax.cs</summary>
// <copyright file="SecuredController.cs" company="Ecole Normale Supérieure">
// Released under GPLv2
// </copyright>
// <summary>Contains a class which represents the application.</summary>

namespace Sqwarea.Api.Web
{
    using System.Reflection;
    using System.Web.Mvc;
    using System.Web.Routing;
    using Autofac.Integration.Web;
    using Autofac.Integration.Web.Mvc;
    using Sqwarea.Web.Tools;

    /// <summary>
    /// Class representing the application
    /// </summary>
    public class MvcApplication : System.Web.HttpApplication, IContainerProviderAccessor
    {
        /// <summary>
        /// The container provider
        /// </summary>
        private static IContainerProvider _containerProvider;

        /// <summary>
        /// Gets the provider used by Autofac
        /// </summary>
        public IContainerProvider ContainerProvider
        {
            get { return _containerProvider; }
        }

        /// <summary>
        /// Register routes for Asp.NET
        /// </summary>
        /// <param name="routes">Routes to register</param>
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Map",
                "map/{stringX}/{stringY}",
                new { controller = "Game", action = "GetSquares" });
            routes.MapRoute(
                "Kings",
                "kings/{ids}",
                new { controller = "Game", action = "GetKings" });

            routes.MapRoute(
                "Default",                                                          // Route name
                "{controller}/{action}/{id}",                                       // URL with parameters
                new { controller = "Home", action = "Index", id = string.Empty });  // Parameter defaults
        }

        /// <summary>
        /// Called when application start
        /// </summary>
        protected void Application_Start()
        {
            var currentDir = System.Web.HttpContext.Current.Server.MapPath("/");
            SqwareaAutofacConfiguration.ConfigureWeb(Assembly.GetExecutingAssembly(), currentDir, out _containerProvider);

            // ...and notify ASP.NET MVC that it should use Autofacs own controller factory to instantiate controllers,
            // so that Autofac can resolve any dependencies the controllers need
            ControllerBuilder.Current.SetControllerFactory(new AutofacControllerFactory(_containerProvider));

            RegisterRoutes(RouteTable.Routes);
        }
    }
}